<!DOCTYPE html>
<html lang="zh-CN">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>文件上传测试</title>
  <style>
    body {
      font-family: Arial, sans-serif;
      max-width: 1200px;
      margin: 0 auto;
      padding: 20px;
    }
    .upload-form {
      margin-bottom: 20px;
      padding: 20px;
      border: 1px solid #ccc;
      border-radius: 5px;
    }
    .upload-form h2 {
      margin-top: 0;
    }
    .upload-form input[type="file"] {
      margin-bottom: 10px;
    }
    .upload-form button {
      padding: 10px 15px;
      background-color: #4CAF50;
      color: white;
      border: none;
      border-radius: 5px;
      cursor: pointer;
    }
    .upload-form button:hover {
      background-color: #45a049;
    }
    #response {
      margin-top: 20px;
      padding: 10px;
      border: 1px solid #ddd;
      border-radius: 5px;
      background-color: #f9f9f9;
    }
    .file-list {
      margin-top: 20px;
    }
    .file-item {
      display: flex;
      align-items: center;
      padding: 10px;
      border: 1px solid #ddd;
      margin-bottom: 10px;
      border-radius: 5px;
    }
    .file-info {
      flex-grow: 1;
    }
    .file-name {
      font-weight: bold;
      margin-bottom: 5px;
    }
    .file-meta {
      color: #666;
      font-size: 0.9em;
    }
    .file-actions {
      margin-left: 20px;
    }
    .delete-btn {
      padding: 5px 10px;
      background-color: #f44336;
      color: white;
      border: none;
      border-radius: 3px;
      cursor: pointer;
    }
    .delete-btn:hover {
      background-color: #d32f2f;
    }
    .loading {
      text-align: center;
      padding: 20px;
      color: #666;
    }
  </style>
</head>
<body>
  <h1>文件上传测试</h1>
  
  <div class="upload-form">
    <h2>单文件上传</h2>
    <form id="singleUploadForm">
      <input type="file" name="file" required>
      <button type="submit">上传</button>
    </form>
  </div>

  <div class="upload-form">
    <h2>多文件上传</h2>
    <form id="multipleUploadForm">
      <input type="file" name="files" multiple required>
      <button type="submit">上传</button>
    </form>
  </div>

  <div id="response"></div>

  <div class="file-list">
    <h2>文件列表</h2>
    <div id="fileList"></div>
  </div>

  <script>
    const responseDiv = document.getElementById('response');
    const fileListDiv = document.getElementById('fileList');

    // 加载文件列表
    async function loadFileList() {
      try {
        fileListDiv.innerHTML = '<div class="loading">加载中...</div>';
        const response = await fetch('/upload/files');
        const result = await response.json();
        
        if (result.success) {
          if (result.data.length === 0) {
            fileListDiv.innerHTML = '<div class="loading">暂无文件</div>';
            return;
          }

          fileListDiv.innerHTML = result.data.map(file => `
            <div class="file-item" data-id="${file.id}">
              <div class="file-info">
                <div class="file-name">${file.originalname}</div>
                <div class="file-meta">
                  大小: ${formatFileSize(file.size)} | 
                  类型: ${file.mimetype} | 
                  上传时间: ${new Date(file.created_at).toLocaleString()}
                </div>
              </div>
              <div class="file-actions">
                <button class="delete-btn" onclick="deleteFile(${file.id})">删除</button>
              </div>
            </div>
          `).join('');
        } else {
          fileListDiv.innerHTML = '<div class="loading">加载失败</div>';
        }
      } catch (error) {
        console.error('加载文件列表失败:', error);
        fileListDiv.innerHTML = '<div class="loading">加载失败</div>';
      }
    }

    // 格式化文件大小
    function formatFileSize(bytes) {
      if (bytes === 0) return '0 B';
      const k = 1024;
      const sizes = ['B', 'KB', 'MB', 'GB'];
      const i = Math.floor(Math.log(bytes) / Math.log(k));
      return parseFloat((bytes / Math.pow(k, i)).toFixed(2)) + ' ' + sizes[i];
    }

    // 删除文件
    async function deleteFile(id) {
      if (!confirm('确定要删除这个文件吗？')) return;

      try {
        const response = await fetch(`/upload/files/${id}`, {
          method: 'DELETE'
        });
        const result = await response.json();
        
        if (result.success) {
          alert('文件删除成功');
          loadFileList(); // 重新加载文件列表
        } else {
          alert('删除失败: ' + result.message);
        }
      } catch (error) {
        console.error('删除文件失败:', error);
        alert('删除文件失败');
      }
    }

    // 单文件上传
    document.getElementById('singleUploadForm').addEventListener('submit', async (e) => {
      e.preventDefault();
      const formData = new FormData(e.target);
      try {
        responseDiv.textContent = '上传中...';
        const response = await fetch('/upload/single', {
          method: 'POST',
          body: formData
        });
        const result = await response.json();
        responseDiv.textContent = JSON.stringify(result, null, 2);
        if (result.success) {
          loadFileList(); // 重新加载文件列表
        }
      } catch (error) {
        responseDiv.textContent = `上传失败: ${error.message}`;
      }
    });

    // 多文件上传
    document.getElementById('multipleUploadForm').addEventListener('submit', async (e) => {
      e.preventDefault();
      const formData = new FormData(e.target);
      try {
        responseDiv.textContent = '上传中...';
        const response = await fetch('/upload/multiple', {
          method: 'POST',
          body: formData
        });
        const result = await response.json();
        responseDiv.textContent = JSON.stringify(result, null, 2);
        if (result.success) {
          loadFileList(); // 重新加载文件列表
        }
      } catch (error) {
        responseDiv.textContent = `上传失败: ${error.message}`;
      }
    });

    // 页面加载时获取文件列表
    loadFileList();
  </script>
</body>
</html> 